var app = {
    /**
     * @description 会员登录
     * @param {Object} obj
     */
    UserLogin: function (obj) {
        var j = $('.login_btn');
        var e = $('#loginTips');
        if (j.val() == '登录中...') return;
        j.val('登录中...');
        var mname = $('#mname').val();
        var password = $('#password').val();
        if (!password || !mname) {
            j.val('登录');
            e.html('&nbsp; 缺少必填内容').fadeIn();
            return;
        }
        $.ajax({
            url: '/user/Login/index',
            type: 'post',
            dataType: 'json',
            data: 'mname=' + mname + '&password=' + password,
            success: function (msg) {
                if (msg.status == 1) {
                    window.location.href = msg.url;
                } else {
                    e.html('&nbsp; ' + msg.info).fadeIn();
                    j.val('登录');
                }
            }
        });
    },
    /**
     * @description 学员注册
     * @example
     * UserReg();
     * @param {Object} obj
     */
    UserReg: function (obj) {
        var e = $('#loginTips');
        var phone = $("#userName").val();
        if (phone == "") {
            e.html("请输入手机号或用户名!").fadeIn();
            return;
        }
//		else if(!/^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/.test(phone) || phone.length != 11) {
//			e.html("请输入正确的手机号!").fadeIn();
//			return;
//		}
        var password = $('#password').val();
        var password2 = $('#againPwd').val();
        if (!password || !password2) {
            e.html('&nbsp; 缺少必填内容').fadeIn();
            return;
        }
        if (password != password2) {
            e.html('&nbsp; 两次输入的密码不一致').fadeIn();
            return;
        }
        $.ajax({
            url: '/user/Login/register',
            type: 'post',
            dataType: 'json',
            data: 'mname=' + phone + '&password=' + password + '&password2=' + password2,
            success: function (msg) {
                if (msg.status == 1) {
                    window.location.href = msg.url;
                } else {
                    e.html('&nbsp; ' + msg.info).fadeIn();
                }
            }
        });
    },
    /**
     * @description 添加数据收藏
     * @param {Object} dataID
     * @param {Object} obj
     */
    fav: function (dataID, obj) {
        if (IS_LOGIN == 2) {
            alert("请先登录或注册");
            window.location.href = '/user/Login';
            return false;
        }
        $.ajax({
            url: '/user/my/addfav',
            type: 'post',
            dataType: 'json',
            data: 'dataid=' + dataID,
            success: function (msg) {

                if (msg.type == 1) {
                    $(obj).addClass('favit');
                    $(obj).removeClass('unfavit');
                } else if (msg.type == 2) {
                    $(obj).addClass('unfavit');
                    $(obj).removeClass('favit');
                } else {
                    alert(msg.info);
                }

            }
        });
    },
    alipaySubmit: function () {
        if (!$(":input[name='price']").val()) {
            $(":input[name='price']").siblings('.errorTips')
                .html('<span class="iconfont">&#xe604;</span><em>请填写充值金额</em>').show();
            return false;
        } else if ($(":input[name='price']").val() < 0) {
            $(":input[name='price']").siblings('.errorTips')
                .html('<span class="iconfont">&#xe604;</span><em>请填写正确的充值金额</em>').show();
            return false;
        }
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'layui-layer-nobg', //加上边框
            area: ['402px', '282px'], //宽高
            content: $('#payQuestion').html()
        });
    },
    wySubmit: function () {
        var obj = $("#money2");
        if (!obj.val()) {
            obj.siblings('.errorTips')
                .html('<span class="iconfont">&#xe604;</span><em>请填写充值金额</em>').show();
            return false;
        } else if (obj.val() < 0) {
            obj.siblings('.errorTips')
                .html('<span class="iconfont">&#xe604;</span><em>请填写正确的充值金额</em>').show();
            return false;
        }
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'layui-layer-nobg', //加上边框
            area: ['402px', '282px'], //宽高
            content: $('#payQuestion').html()
        });
    },
    /**
     * @param {Object} msg
     * @param {Object} selector
     */
    showError: function (msg, selector) {
        selector.next('.errorTips').show().find('em').html(msg);
    },
    pwdForm: function () {
        // 隐藏错误提示
        $('.errorTips').hide();

        if (!$(':input[name=opd]').val()) {
            $(':input[name=opd]').focus();
            $(':input[name=opd]').siblings('.errorTips').show().find('em').html('请输入原始密码');
            return false;
        } else if (!$(':input[name=npd]').val()) {
            $(':input[name=npd]').focus();
            $(':input[name=npd]').siblings('.errorTips').show().find('em').html('请输入新建密码');
            return false;
        } else if (!$(':input[name=npd2]').val()) {
            $(':input[name=npd2]').focus();
            $(':input[name=npd2]').siblings('.errorTips').show().find('em').html('请输入确认密码');
            return false;
        } else if ($(':input[name=npd]').val() !== $(':input[name=npd2]').val()) {
            $(':input[name=npd]').focus();
            $('.pwdForm').find('.centerBtn').siblings('.errorTips').show().find('em').html('两次输入的密码不一致');
            return false;
        }
        $.ajax({
            url: '/user/my/editPwd',
            type: 'post',
            dataType: 'json',
            data: $('#form').serialize(),
            success: function (json) {
                if (json.state == 1) {
                    layer.alert(json.info);
                } else {
                    layer.alert(json.info);
                }

            }
        });
    },
    accountInfo: function () {
        $.ajax({
            url: '/user/my/accountInfo',
            type: 'post',
            dataType: 'json',
            data: $('#form').serialize(),
            success: function (json) {
                layer.alert(json.info);
            }
        });
    },
    freeapi: function (aid) {
        $.ajax({
            url: '/user/my/free',
            type: 'post',
            dataType: 'json',
            data: 'aid=' + aid,
            success: function (json) {
                layer.alert(json.info, null, function () {
                    if (json.url) {
                        window.location.href = json.url;
                    } else {
                        layer.closeAll();
                    }
                });
            }
        });
    },
    pass: function () {
        alert('已关闭找回密码功能,如您忘记密码,请联系客服');
    }
}
$(function () {
    $("input[name=q]").focus(function () {
        document.onkeydown = function (e) {
            var ev = document.all ? window.event : e;
            if (ev.keyCode == 13) {
                $("#searchBtn").click();
            }
        }
    })
    // header里的搜索框
    $("#searchBtn").click(function () {
        var q = $("input[name=q]").val();
        q = q.replace(/\//g, '');

        if (q.length == '') {
            $("input[name=q]").focus();
            return false;
        }
        window.location.href = '/user/my/data/?key=' + q;
    })
});